package com.amphora.web.actions;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.amphora.model.Category;
import com.amphora.model.TreatmentType;
import com.amphora.services.application.BusinessServices;
import com.amphora.services.application.impl.BusinessServicesImpl;

@WebServlet("/TreatmentsAction")
public class TreatmentsAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static String VIEW_TREATMENTS = "/view_treatments.jsp";
	private static String ERROR_ = "/error.jsp";
	private BusinessServices treatmentController;
	
	public void init(ServletConfig config) throws ServletException {
		treatmentController = new BusinessServicesImpl();
		System.out.println("TreatmentsAction init method");
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("TreatmentsAction get method");
		Map parameters = request.getParameterMap();
		String forward = "";
		
		if(parameters.containsKey("view")){
			List<TreatmentType> allTreatments = treatmentController.viewAllTreatmentType();
			System.out.println("Amount of treatment types found: " + allTreatments.size());
			
			List<Category> allCategories = treatmentController.viewAllCategories();
			System.out.println("Amount of categories found: " + allCategories.size());

			request.setAttribute("treatment", allTreatments);
			request.setAttribute("categories", allCategories);
			forward = VIEW_TREATMENTS;
			
		}else{
			forward = ERROR_;
		}
		
		RequestDispatcher view = request.getRequestDispatcher(forward);
		view.forward(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("TreatmentsAction post method");
	}

}
